﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cecbData;
using cecbClasses;

namespace cecbClasses.Data_Adapters.LeopoldMetrixCharts
{
    public class LeopoldMetrixCharts
    {
        public LeopoldMetrixCharts()
        {
            cecbContext = new DSSEntities();
        }
        DSSEntities cecbContext;
        double sum;

        public double getPhysicalEnvironmentalAffection(string project , string component)
        {
            sum = getSummation(project);
            double phisicalImpact = 0;


           
                if (sum != 0)
                {

                    var count1 = (from c in cecbContext.LeopoldMatrixValues
                                  join b in cecbContext.Impacts on c.impt_reference equals b.impt_reference
                                  join g in cecbContext.Components on b.impt_component equals g.cmpt_reference
                                  where c.proj_reference.Contains(project)
                                  && g.cmpt_mainCat.Contains(component) && c.proj_reference.Contains(project)

                                  group c by new
                                  {
                                      c.proj_reference
                                  }

                                      into cc
                                      select cc.Sum(c => c.mtrx_importance*c.mtrx_magnitude)  ).FirstOrDefault();

                    try
                    {
                        phisicalImpact = count1.Value;
                        phisicalImpact = Math.Round(phisicalImpact, 2);
                    }
                    catch (Exception)
                    {
                        phisicalImpact = 0.00;

                    }

                }


           return phisicalImpact;
        }




        public double getPhysicalEnvironmentalAffectionPresentage(string project ,  string component)
        {
            double presentage = 0;
            sum = getSummation(project);

            
            
                if (sum != 0)
                {

                    var quary = (from c in cecbContext.LeopoldMatrixValues
                                 join b in cecbContext.Impacts on c.impt_reference equals b.impt_reference
                                 join g in cecbContext.Components on b.impt_component equals g.cmpt_reference
                                 where c.proj_reference.Contains(project)
                                 && g.cmpt_mainCat.Contains(component)

                                 group c by new
                                 {
                                     c.proj_reference
                                 }

                                     into cc
                                     select cc.Sum(c => c.mtrx_importance*c.mtrx_magnitude) / sum).FirstOrDefault();

                    try
                    {
                        presentage = quary.Value;
                    }
                    catch (Exception)
                    {
                        presentage = 2;

                    }


                }

                presentage = Math.Round(presentage, 2) * 100;
           

            return presentage;
        }
        public double getSummation(string project)
        {
            try
            {
                var quary = (from c in cecbContext.LeopoldMatrixValues

                             where c.proj_reference.Contains(project)



                             group c by new
                             {
                                 c.proj_reference
                             }
                                 into cc
                                 select cc.Sum(c => c.mtrx_importance*c.mtrx_magnitude)).FirstOrDefault();
                sum = quary.Value;
            }
            catch (Exception)
            {


            }

            return sum;
        }


    }
       
}
